package top.sssd.auth.controller.index;

import cloud.tianai.captcha.application.ImageCaptchaApplication;
import cloud.tianai.captcha.application.vo.CaptchaResponse;
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.common.response.ApiResponse;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.crypto.digest.MD5;
import org.springframework.web.bind.annotation.*;
import top.sssd.auth.common.CommonResponse;
import top.sssd.auth.model.index.request.CaptchaReq;
import top.sssd.auth.model.index.request.LoginUserReq;
import top.sssd.auth.model.user.entity.User;
import top.sssd.auth.service.user.UserService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Objects;

@RestController
@RequestMapping("/index")
public class IndexController {
    @Resource
    private ImageCaptchaApplication captchaApplication;
    @Resource
    private UserService userService;

    @GetMapping("/captcha")
    public CaptchaResponse<ImageCaptchaVO> genCaptcha() {
        // 1.生成验证码(该数据返回给前端用于展示验证码数据)
        // 参数1为具体的验证码类型， 默认支持 SLIDER、ROTATE、WORD_IMAGE_CLICK、CONCAT 等验证码类型，详见： `CaptchaTypeConstant`类
        return captchaApplication.generateCaptcha(CaptchaTypeConstant.SLIDER);
    }

    @PostMapping("/captcha")
    public CommonResponse<Boolean> captcha(@RequestBody CaptchaReq captchaReq) {
        ApiResponse<?> response = captchaApplication.matching(captchaReq.getId(), captchaReq.getTrack());
        if (response.isSuccess()) {
            return CommonResponse.ok(Boolean.TRUE);
        } else {
            return CommonResponse.fail(Boolean.FALSE);
        }
    }

    @GetMapping("/login")
    public CommonResponse<Boolean> login() {
        return CommonResponse.ok(StpUtil.isLogin());
    }

    @PostMapping("/login")
    public CommonResponse<Boolean> login(@RequestBody LoginUserReq loginUserReq) {
        //校验验证码是否通过
        ApiResponse<?> response = captchaApplication.matching(loginUserReq.getId(), loginUserReq.getTrack());
        if (Boolean.FALSE.equals(response.isSuccess())) {
            return CommonResponse.fail(Boolean.FALSE, "验证码错误");
        }
        User one = userService.lambdaQuery()
                .eq(User::getUsername, loginUserReq.getUsername())
                .last("limit 1")
                .one();
        if (Objects.isNull(one)) {
            return CommonResponse.fail(Boolean.FALSE, "用户不存在");
        }
        String pwdReq
                = Arrays.toString(MD5.create().digest(loginUserReq.getPassword()));
        if (!pwdReq.equals(one.getPassword())) {
            return CommonResponse.fail(Boolean.FALSE, "用户名或密码不正确!");
        }
        if (Boolean.FALSE.equals(one.getStatus())) {
            return CommonResponse.fail(Boolean.FALSE, "用户已被禁用");
        }
        StpUtil.login(one.getId());
        return CommonResponse.ok(Boolean.TRUE);
    }

    @DeleteMapping("/logout")
    public CommonResponse<Boolean> logout() {
        StpUtil.logout();
        return CommonResponse.ok(Boolean.TRUE);
    }

}
